package xcmg.device.service.warehouse;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.mapper.CompanyInfoMapper;
import xcmg.device.util.CompanyUtil;
import yb.ecp.fast.dto.UserCacheDTO;
import yb.ecp.fast.feign.AuthClient;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.List;

@Service
public class CommonService {

    @Autowired
    private AuthClient authClient;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;

    @Autowired
    private CompanyUtil companyUtil;

    /**
     * 根据用户部门ID查询其对应的销售组织
     * @param deptId
     * @return
     */
    public List<String> getSalesCodes(String deptId) {
        if (StringUtils.isBlank(deptId)) {
            return Lists.newArrayList();
        }
        return companyInfoMapper.getSalesCodes(companyUtil.ynOrgId(),deptId);
    }

    public boolean checkIsYnOrgId(String orgId) {
        return orgId.equals(companyUtil.ynOrgId());
    }


    /**
     * @Author: WangKe
     * @Description: 获取用户名
     * @Date: 2018/2/6 0006
     */
    public String getUserName(String userId){
        String userName;
        try {
            ActionResult dd =  authClient.getUserDetail(userId);
            UserCacheDTO userInfo = (UserCacheDTO) dd.getValue();
            userName = userInfo.getName();
        }catch (Exception e){
            userName = userId;
        }
        return userName;
    }

    /**
     * @Author: wangyong
     * @Description: 获取用户信息
     * @Date: 2019/04/16
     */
    public UserCacheDTO getUserInfo(String userId) throws Exception {
        UserCacheDTO userInfo;
        try {
            ActionResult dd = authClient.getUserDetail(userId);
            userInfo = (UserCacheDTO) dd.getValue();

        } catch (Exception e) {
            throw new Exception("获取用户信息失败，userId: " + userId);
        }
        return userInfo;
    }

}
